package com.bsj.power.sdk.config.hksdk.alarm;

import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.mq.sdk.SdkLoginDevInfo;
import com.bsj.power.common.def.mq.sdk.face.SdkFaceLoginDevInfo;
import com.bsj.power.sdk.config.hksdk.HCNetSDK;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

import static com.bsj.power.sdk.config.hksdk.HKSDKClientManage.hCNetSDK;

/**
 * @author yinhao
 * @version 1.0
 * @description 报警布防/撤防
 * @date 2024/8/28
 */
@Slf4j
public class AlarmSet {

    /**
     * 布防
     *
     * @param sdkLoginDevInfo
     * @return
     */
    public static void arrange(SdkLoginDevInfo sdkLoginDevInfo, JsonResult result) {
        int lAlarmHandle = sdkLoginDevInfo.getLAlarmHandle();
        if (lAlarmHandle < 0) {
            //尚未布防,需要布防
            //报警布防参数设置
            HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
            m_strAlarmInfo.dwSize = m_strAlarmInfo.size();
            //布防等级
            m_strAlarmInfo.byLevel = 0;
            // 智能交通报警信息上传类型：0- 老报警信息（NET_DVR_PLATE_RESULT），1- 新报警信息(NET_ITS_PLATE_RESULT)
            m_strAlarmInfo.byAlarmInfoType = 1;
            //布防类型：0-客户端布防，1-实时布防
            m_strAlarmInfo.byDeployType = 0;
            m_strAlarmInfo.write();
            lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(sdkLoginDevInfo.getUserId(), m_strAlarmInfo);
            log.info("设备布防设备ID: {}, lAlarmHandle: {}", sdkLoginDevInfo.getId(), lAlarmHandle);
            if (lAlarmHandle == -1) {
                log.info("布防失败，错误码为: {}", hCNetSDK.NET_DVR_GetLastError());
                result.setMsg("布防失败，错误码为: " + hCNetSDK.NET_DVR_GetLastError());
                return;
            } else {
                log.info("设备布防成功，设备ID： {}", sdkLoginDevInfo.getId());
                sdkLoginDevInfo.setLAlarmHandle(lAlarmHandle);
                result.setMsg("布防成功");
                return;
            }
        } else {
            log.info("设备已经布防，请先撤防！");
            result.setMsg("设备已经布防，请先撤防！");
            return;
        }
    }

    /**
     * 撤防
     *
     * @param sdkLoginDevInfo
     */
    public static void withdraw(SdkLoginDevInfo sdkLoginDevInfo) {
        int lAlarmHandle = sdkLoginDevInfo.getLAlarmHandle();
        if (lAlarmHandle > -1) {
            if (hCNetSDK.NET_DVR_CloseAlarmChan(lAlarmHandle)) {
                sdkLoginDevInfo.setLAlarmHandle(-1);
                log.info("设备ID：{}, 撤防成功", sdkLoginDevInfo.getId());
            }
        }
    }


    /**
     * 面板机布防
     *
     * @param sdkFaceLoginDevInfo
     * @return
     */
    public static void faceArrange(SdkFaceLoginDevInfo sdkFaceLoginDevInfo, JsonResult result) {
        int lAlarmHandle = sdkFaceLoginDevInfo.getLAlarmHandle();
        if (lAlarmHandle < 0)//尚未布防,需要布防
        {
            //报警布防参数设置
            HCNetSDK.NET_DVR_SETUPALARM_PARAM alarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
            alarmInfo.dwSize = alarmInfo.size();
            alarmInfo.byLevel = 0;  //布防等级
            alarmInfo.byAlarmInfoType = 1;   // 智能交通报警信息上传类型：0- 老报警信息（NET_DVR_PLATE_RESULT），1- 新报警信息(NET_ITS_PLATE_RESULT)
            alarmInfo.byDeployType = 1;   //布防类型：0-客户端布防，1-实时布防，客户端布防仅支持一路
            alarmInfo.write();
            lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(sdkFaceLoginDevInfo.getUserId(), alarmInfo);
            if (lAlarmHandle == -1) {
                log.info("面板机实时布防失败，错误码为: {}，登录信息为：{}", hCNetSDK.NET_DVR_GetLastError(), sdkFaceLoginDevInfo);
                result.setMsg("面板机实时布防失败，错误码为: " + hCNetSDK.NET_DVR_GetLastError());
            } else {
                log.info("面板机实时布防成功，登录信息为：{}", sdkFaceLoginDevInfo);
                sdkFaceLoginDevInfo.setLAlarmHandle(lAlarmHandle);
                result.setMsg("布防成功");
            }
        } else {
            log.info("面板机设备已经布防，请先撤防！登录信息为：{}", sdkFaceLoginDevInfo);
            result.setMsg("设备已经布防，请先撤防！");
        }
    }

//    /**
//     * 面板机布防
//     *
//     * @param sdkFaceLoginDevInfo
//     * @return
//     */
//    public static void faceArrange(SdkFaceLoginDevInfo sdkFaceLoginDevInfo, JsonResult result) {
//        int lAlarmHandle = sdkFaceLoginDevInfo.getLAlarmHandle();
//        if (lAlarmHandle < 0) {
//            //尚未布防,需要布防
//            //报警布防参数设置
//            HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
//            m_strAlarmInfo.dwSize = m_strAlarmInfo.size();
//            //布防等级
//            m_strAlarmInfo.byLevel = 0;
//            // 智能交通报警信息上传类型：0- 老报警信息（NET_DVR_PLATE_RESULT），1- 新报警信息(NET_ITS_PLATE_RESULT)
//            m_strAlarmInfo.byAlarmInfoType = 1;
//            //布防类型：0-客户端布防，1-实时布防
//            m_strAlarmInfo.byDeployType = 0;
//            m_strAlarmInfo.write();
//            lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(sdkFaceLoginDevInfo.getUserId(), m_strAlarmInfo);
//            if (lAlarmHandle == -1) {
//                log.info("面板机布防失败，错误码为: {}，登录信息为：{}", hCNetSDK.NET_DVR_GetLastError(), sdkFaceLoginDevInfo);
//                result.setMsg("布防失败，错误码为: " + hCNetSDK.NET_DVR_GetLastError());
//                return;
//            } else {
//                log.info("面板机设备布防成功，登录信息为：{}", sdkFaceLoginDevInfo);
//                sdkFaceLoginDevInfo.setLAlarmHandle(lAlarmHandle);
//                result.setMsg("布防成功");
//                return;
//            }
//        } else {
//            log.info("面板机设备已经布防，请先撤防！登录信息为：{}", sdkFaceLoginDevInfo);
//            result.setMsg("设备已经布防，请先撤防！");
//            return;
//        }
//    }

    /**
     * 面板机撤防
     *
     * @param sdkFaceLoginDevInfo
     */
    public static void faceWithdraw(SdkFaceLoginDevInfo sdkFaceLoginDevInfo) {
        int lAlarmHandle = sdkFaceLoginDevInfo.getLAlarmHandle();
        if (lAlarmHandle > -1) {
            if (hCNetSDK.NET_DVR_CloseAlarmChan(lAlarmHandle)) {
                sdkFaceLoginDevInfo.setLAlarmHandle(-1);
                log.info("面板机撤防成功");
            }
        }
    }

}
